<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\controller\v1\dealer;

use app\api\controller\ApiBase;
use app\api\model\dealer\Apply as ApplyModel;
use app\api\model\dealer\Referee as RefereeModel;
use app\api\service\user\User as UserService;

class Apply extends ApiBase {

    protected $model;

    public function __construct(ApplyModel $model){

        parent::__construct();
        $this->model = $model;

    }

    public function getStatus(){
        // 获取当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        $apply = $this->model->findData(['user_id' => $userId]);

        $status = 0;//未申请
        $applyStatus = -1;//未申请
        if($apply){
            $applyStatus = $apply['apply_status'];//审核状态 (10待审核 20审核通过 30驳回)
        }

        return success('', [
            'applyStatus' => $applyStatus
        ]);
    }


    public function add(){
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $userId = UserService::getCurrentLoginUserId();

        // 数据整理
        $data = [
            'user_id' => $userId,
            'name' => trim($name),
            'mobile' => trim($mobile),
            'referee_id' => RefereeModel::getRefereeUserId($userId, 1),
            //'apply_type' => $config['become'],
            'apply_type' => 10, //自动审核
            'apply_time' => time()
        ];

        if($this->model->saveData($data,false)){
            return success('申请已提交!');//分销商申请已提交
        }else{
            return error('提交失败!');
        }

    }


}